﻿//-------------
// <copyright file="UserMapper.cs" company="Uncas">
//     Copyright (c) Ole Lynge Sørensen. All rights reserved.
// </copyright>
//-------------

namespace Uncas.Katas.SimpleMembership.Web.Mappers
{
    using System.Linq;
    using AutoMapper;
    using Uncas.Katas.SimpleMembership.Domain;
    using Uncas.Katas.SimpleMembership.Web.ViewModels;

    /// <summary>
    /// Maps user.
    /// </summary>
    public static class UserMapper
    {
        /// <summary>
        /// Configures this instance.
        /// </summary>
        public static void Configure()
        {
            Mapper.CreateMap<User, UserListItemViewModel>();
        }

        /// <summary>
        /// Maps the user to view model.
        /// </summary>
        /// <param name="user">The provided user.</param>
        /// <returns>The resulting view model.</returns>
        public static UserListItemViewModel MapUserToViewModel(
            User user)
        {
            var result = Mapper.Map<User, UserListItemViewModel>(user);
            if (user.Roles != null && user.Roles.Count() != 0)
            {
                result.Roles = user.Roles.Aggregate((running, item) => running += ", " + item);
            }
            else
            {
                result.Roles = string.Empty;
            }

            return result;
        }
    }
}